/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.ui.detail;

import com.siro.pm.I18nManager;
import com.siro.pm.model.Developer;
import com.siro.pm.model.Project;
import com.siro.pm.model.provider.DeveloperProvider;
import com.siro.pm.ui.main.DevelopersPage;
import com.siro.pm.utils.DeveloperBrowser;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since AddDeveloperPage.java v1.0
 */
public class AddDeveloperPage extends VerticalLayout {
	private static final long serialVersionUID = -9161029111469664060L;

	private Window window;
	private DevelopersPage developersPage;
	private Project ownerProject;

	ComboBox creatorDeveloper;
	private Button button_Save;

	public AddDeveloperPage() {
	}

	public AddDeveloperPage(Project ownerProject) {
		this.ownerProject = ownerProject;
	}

	public void createContents() {
		setMargin(true);
		setSpacing(true);

		// base info
		GridLayout developerBaseInfo = new GridLayout();
		developerBaseInfo.setStyleName("h2");
		developerBaseInfo.setCaption(I18nManager.getMessage("developer.add"));
		developerBaseInfo.setMargin(true);
		developerBaseInfo.setSpacing(true);
		developerBaseInfo.setWidth("100%");

		HorizontalLayout developerLayout = new HorizontalLayout();
		developerLayout.setWidth("100%");
		creatorDeveloper = new ComboBox(" ");
		creatorDeveloper.setWidth("100%");
		DeveloperBrowser.configDeveloperBrowser(creatorDeveloper, null);
		developerLayout.addComponent(creatorDeveloper);
		developerBaseInfo.addComponent(developerLayout);

		// buttons
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setMargin(true);
		buttons.setSpacing(true);
		buttons.setWidth("100%");

		button_Save = new Button(I18nManager.getMessage("button.save"));
		button_Save.setWidth(50, Sizeable.UNITS_PIXELS);
		button_Save.addListener(new SaveClickListener());
		buttons.addComponent(button_Save);
		buttons.setComponentAlignment(button_Save, Alignment.MIDDLE_RIGHT);

		addComponent(developerBaseInfo);
		addComponent(buttons);
	}

	class SaveClickListener implements ClickListener {
		private static final long serialVersionUID = -2851840644753744125L;

		public void buttonClick(ClickEvent event) {
			Developer developer = (Developer) creatorDeveloper.getValue();
			if (developer != null) {
				Project.addDeveloper(ownerProject, developer);
				// ownerProject.getDevelopers().add(developer);
			}
			developersPage.developersTable
					.setContainerDataSource(DeveloperProvider
							.getDevelopersData(developersPage.getProject()
									.getDevelopers()));
			(window.getParent()).removeWindow(window);
		}
	}

	public Window getDeveloperWindow() {
		return window;
	}

	public void setWindow(Window window) {
		this.window = window;
	}

	public DevelopersPage getDevelopersPage() {
		return developersPage;
	}

	public void setDevelopersPage(DevelopersPage developersPage) {
		this.developersPage = developersPage;
	}

	public Project getOwnerProject() {
		return ownerProject;
	}

	public void setOwnerProject(Project ownerProject) {
		this.ownerProject = ownerProject;
	}
}
